package com.grass.appointment.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.media.ThumbnailUtils;
import android.os.Build;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.androidx.p013lv.base.dialog.ProgressBarDialog;
import com.androidx.p013lv.base.http.BaseRes;
import com.androidx.p013lv.base.interfaces.Key;
import com.androidx.p013lv.base.p015ui.BaseActivity;
import com.androidx.p013lv.base.utils.GlideEngine;
import com.androidx.p013lv.base.utils.PictureStyle;
import com.androidx.p013lv.base.utils.ToastUtils;
import com.grass.appointment.C2694R;
import com.grass.appointment.adapter.SendImageAdapter;
import com.grass.appointment.bean.ReqAddVideoBean;
import com.grass.appointment.bean.SendVideoBean;
import com.grass.appointment.databinding.ActivitySendDynamicLayoutBinding;
import com.grass.appointment.model.SendDynamicModel;
import com.grass.appointment.model.SendModel;
import com.gyf.immersionbar.ImmersionBar;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.permissions.PermissionChecker;
import com.luck.picture.lib.tools.PictureFileUtils;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import p000a.p002b.RxPermissions;
import p023io.reactivex.functions.Consumer;

public class SendDynamicActivity extends BaseActivity<ActivitySendDynamicLayoutBinding> implements AdapterView.OnItemClickListener {
    private static final int selectedNum = 9;
    private SendImageAdapter adapter;
    private final String addImage = Key.ADD_IMAGE;
    private ProgressBarDialog dialog;
    private int duration;
    private List<String> imageList;
    private SendVideoBean mVideoBean;
    private SendDynamicModel model;
    private SendModel sendModel;
    public int type;
    private String videoPath;

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this).titleBar(((ActivitySendDynamicLayoutBinding) this.binding).toolbar).init();
    }

    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public int setLayout() {
        return C2694R.C2699layout.activity_send_dynamic_layout;
    }

    /* access modifiers changed from: protected */
    @Override // com.androidx.p013lv.base.p015ui.BaseActivity
    public void initView() {
        super.initView();
        getWindow().setSoftInputMode(2);
        this.model = (SendDynamicModel) new ViewModelProvider(this).get(SendDynamicModel.class);
        this.sendModel = (SendModel) new ViewModelProvider(this).get(SendModel.class);
        this.dialog = new ProgressBarDialog(this);
        OnClick onClick = new OnClick();
        ((ActivitySendDynamicLayoutBinding) this.binding).setClick(onClick);
        ((ActivitySendDynamicLayoutBinding) this.binding).setBtnStatus(0);
        ((ActivitySendDynamicLayoutBinding) this.binding).setEditNum(0);
        ((ActivitySendDynamicLayoutBinding) this.binding).setType(Integer.valueOf(this.type));
        ArrayList arrayList = new ArrayList();
        this.imageList = arrayList;
        arrayList.add(Key.ADD_IMAGE);
        SendImageAdapter sendImageAdapter = new SendImageAdapter(this);
        this.adapter = sendImageAdapter;
        sendImageAdapter.setOnClickListener(new View.OnClickListener() {
            /* class com.grass.appointment.activity.SendDynamicActivity.View$OnClickListenerC27221 */

            public void onClick(View view) {
                SendDynamicActivity.this.deleteImageForPosition(view);
            }
        });
        ((ActivitySendDynamicLayoutBinding) this.binding).gridView.setAdapter((ListAdapter) this.adapter);
        ((ActivitySendDynamicLayoutBinding) this.binding).gridView.setOnItemClickListener(this);
        this.adapter.setImageList(this.imageList);
        ((ActivitySendDynamicLayoutBinding) this.binding).gridView.setOnTouchListener(new View.OnTouchListener() {
            /* class com.grass.appointment.activity.SendDynamicActivity.View$OnTouchListenerC27232 */

            public boolean onTouch(View view, MotionEvent motionEvent) {
                SendDynamicActivity.this.onTouchEvent(motionEvent);
                return false;
            }
        });
        ((ActivitySendDynamicLayoutBinding) this.binding).scrollView.setOnTouchListener(new View.OnTouchListener() {
            /* class com.grass.appointment.activity.SendDynamicActivity.View$OnTouchListenerC27243 */

            public boolean onTouch(View view, MotionEvent motionEvent) {
                SendDynamicActivity.this.onTouchEvent(motionEvent);
                return false;
            }
        });
        ((ActivitySendDynamicLayoutBinding) this.binding).tvEdit.addTextChangedListener(new TextWatcher() {
            /* class com.grass.appointment.activity.SendDynamicActivity.C27254 */

            public void afterTextChanged(Editable editable) {
            }

            public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
            }

            public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
                ((ActivitySendDynamicLayoutBinding) SendDynamicActivity.this.binding).setEditNum(Integer.valueOf(charSequence.length()));
            }
        });
        this.model.releaseDynamic().observe(this, new Observer<BaseRes<String>>() {
            /* class com.grass.appointment.activity.SendDynamicActivity.C27265 */

            public void onChanged(BaseRes<String> baseRes) {
                SendDynamicActivity.this.dismissProgressBarDialog();
                if (baseRes.getCode() == 200) {
                    ToastUtils.getInstance().showCorrect("發布成功");
                    SendDynamicActivity.this.finish();
                    return;
                }
                ToastUtils.getInstance().showWeak("發布失敗");
            }
        });
        this.sendModel.getMutableLiveData().observe(this, new Observer<BaseRes<SendVideoBean>>() {
            /* class com.grass.appointment.activity.SendDynamicActivity.C27276 */

            public void onChanged(BaseRes<SendVideoBean> baseRes) {
                if (baseRes.getCode() == 1000001) {
                    SendDynamicActivity sendDynamicActivity = SendDynamicActivity.this;
                    sendDynamicActivity.showProgressBarDialog("已經上傳：" + ((int) baseRes.getData().getProgressNum()) + "%，請耐心等待");
                } else if (baseRes.getCode() == 1000002) {
                    SendDynamicActivity sendDynamicActivity2 = SendDynamicActivity.this;
                    sendDynamicActivity2.showProgressBarDialog("正在壓縮：" + ((int) baseRes.getData().getProgressNum()) + "%，請耐心等待");
                } else if (baseRes.getCode() == 1000003) {
                    SendDynamicActivity.this.showProgressBarDialog("正在上傳，請稍後...");
                } else if (baseRes.getCode() == 200) {
                    SendDynamicActivity.this.showProgressBarDialog("正在發布，請稍後...");
                    SendDynamicActivity.this.mVideoBean = baseRes.getData();
                    String trim = ((ActivitySendDynamicLayoutBinding) SendDynamicActivity.this.binding).tvEdit.getText().toString().trim();
                    ReqAddVideoBean reqAddVideoBean = new ReqAddVideoBean();
                    reqAddVideoBean.setPlayTime(SendDynamicActivity.this.duration + "");
                    reqAddVideoBean.setId(SendDynamicActivity.this.mVideoBean.getId());
                    reqAddVideoBean.setCheckSum(SendDynamicActivity.this.mVideoBean.getCheckSum());
                    reqAddVideoBean.setSize(SendDynamicActivity.this.mVideoBean.getSize() + "");
                    reqAddVideoBean.setVideoUrl(SendDynamicActivity.this.mVideoBean.getVideoUri());
                    SendDynamicActivity.this.model.sendDynamic(trim, null, reqAddVideoBean);
                } else {
                    SendDynamicActivity.this.dismissProgressBarDialog();
                    if (baseRes.getCode() == 1026) {
                        ToastUtils.getInstance().showSigh("視頻已經上傳過了");
                    } else {
                        ToastUtils.getInstance().showWeak("上傳失敗，請重新選擇視頻上傳");
                    }
                }
            }
        });
        if (this.type == 0) {
            onClick.selectImgDynamic();
        } else {
            onClick.selectVideoDynamic();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void showProgressBarDialog(String str) {
        if (this.dialog == null) {
            this.dialog = new ProgressBarDialog(this);
        }
        this.dialog.setHint(str);
        if (!this.dialog.isShowing()) {
            this.dialog.show();
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void dismissProgressBarDialog() {
        ProgressBarDialog progressBarDialog = this.dialog;
        if (progressBarDialog != null && progressBarDialog.isShowing()) {
            this.dialog.dismiss();
        }
    }

    /* access modifiers changed from: protected */
    @Override // androidx.fragment.app.FragmentActivity
    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
        if (i2 == -1 && i == 188) {
            try {
                if (((ActivitySendDynamicLayoutBinding) this.binding).getType().intValue() == 0) {
                    pictureInformation(intent);
                } else {
                    videoInformation(intent);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void requestPicturePermission(final int i) {
        if (Build.VERSION.SDK_INT >= 23) {
            new RxPermissions(this).request("android.permission.READ_EXTERNAL_STORAGE", "android.permission.CAMERA").subscribe(new Consumer<Boolean>() {
                /* class com.grass.appointment.activity.SendDynamicActivity.C27287 */

                public void accept(Boolean bool) {
                    if (bool.booleanValue()) {
                        SendDynamicActivity.this.selectPicture(i);
                    } else {
                        ToastUtils.getInstance().showSigh("未授權權限，請在設置中允許權限");
                    }
                }
            });
        } else {
            selectPicture(i);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void requestVideoPermission() {
        if (Build.VERSION.SDK_INT >= 23) {
            new RxPermissions(this).request("android.permission.READ_EXTERNAL_STORAGE", "android.permission.CAMERA").subscribe(new Consumer<Boolean>() {
                /* class com.grass.appointment.activity.SendDynamicActivity.C27298 */

                public void accept(Boolean bool) {
                    if (bool.booleanValue()) {
                        SendDynamicActivity.this.selectVideo();
                    } else {
                        ToastUtils.getInstance().showSigh("未授權權限，請在設置中允許權限");
                    }
                }
            });
        } else {
            selectVideo();
        }
    }

    private void clearCache() {
        if (PermissionChecker.checkSelfPermission(this, "android.permission.WRITE_EXTERNAL_STORAGE")) {
            PictureFileUtils.deleteAllCacheDirFile(this);
        } else {
            PermissionChecker.requestPermissions(this, new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"}, 1);
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void selectPicture(int i) {
        PictureSelector.create(this).openGallery(PictureMimeType.ofImage()).imageEngine(GlideEngine.createGlideEngine()).selectionMode(2).setPictureStyle(PictureStyle.getInstance(this).getPictureParameterStyle()).setPictureCropStyle(PictureStyle.getInstance(this).getCropParameterStyle()).maxSelectNum(i).isPreviewImage(true).isCamera(true).forResult(188);
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void selectVideo() {
        PictureSelector.create(this).openGallery(PictureMimeType.ofVideo()).imageEngine(GlideEngine.createGlideEngine()).selectionMode(1).setPictureStyle(PictureStyle.getInstance(this).getPictureParameterStyle()).isPreviewVideo(true).isCamera(true).forResult(188);
    }

    private void pictureInformation(Intent intent) {
        String str;
        Iterator<String> it = this.imageList.iterator();
        while (it.hasNext()) {
            if (it.next().equals(Key.ADD_IMAGE)) {
                it.remove();
            }
        }
        List<LocalMedia> obtainMultipleResult = PictureSelector.obtainMultipleResult(intent);
        if (obtainMultipleResult != null) {
            for (int i = 0; i < obtainMultipleResult.size(); i++) {
                LocalMedia localMedia = obtainMultipleResult.get(i);
                if (TextUtils.isEmpty(localMedia.getAndroidQToPath())) {
                    str = localMedia.getPath();
                } else {
                    str = localMedia.getAndroidQToPath();
                }
                this.imageList.add(str);
            }
        }
        if (this.imageList.size() < 9) {
            this.imageList.add(Key.ADD_IMAGE);
        }
        this.adapter.setImageList(this.imageList);
        if (this.imageList.size() > 1) {
            ((ActivitySendDynamicLayoutBinding) this.binding).setBtnStatus(1);
        }
    }

    private void videoInformation(Intent intent) {
        List<LocalMedia> obtainMultipleResult = PictureSelector.obtainMultipleResult(intent);
        if (obtainMultipleResult != null && obtainMultipleResult.size() > 0) {
            LocalMedia localMedia = obtainMultipleResult.get(0);
            if (TextUtils.isEmpty(localMedia.getAndroidQToPath())) {
                this.videoPath = localMedia.getPath();
            } else {
                this.videoPath = localMedia.getAndroidQToPath();
            }
            this.duration = ((int) localMedia.getDuration()) / 1000;
            ((ActivitySendDynamicLayoutBinding) this.binding).setVideoCover(ThumbnailUtils.createVideoThumbnail(this.videoPath, 3));
            ((ActivitySendDynamicLayoutBinding) this.binding).setBtnStatus(1);
        }
    }

    public static void setVideoCover(ImageView imageView, Bitmap bitmap) {
        imageView.setImageBitmap(bitmap);
    }

    @Override // android.widget.AdapterView.OnItemClickListener
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
        if (this.imageList.get(i).equals(Key.ADD_IMAGE)) {
            requestPicturePermission(9 - (this.imageList.size() - 1));
        }
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private void deleteImageForPosition(View view) {
        if (this.imageList.size() != 0) {
            Object tag = view.getTag();
            if (tag instanceof Integer) {
                this.imageList.remove(((Integer) tag).intValue());
                boolean z = false;
                for (int i = 0; i < this.imageList.size(); i++) {
                    if (this.imageList.get(i).equals(Key.ADD_IMAGE)) {
                        z = true;
                    }
                }
                if (!z) {
                    this.imageList.add(Key.ADD_IMAGE);
                }
                this.adapter.setImageList(this.imageList);
                if (this.imageList.size() > 1) {
                    ((ActivitySendDynamicLayoutBinding) this.binding).setBtnStatus(1);
                } else {
                    ((ActivitySendDynamicLayoutBinding) this.binding).setBtnStatus(0);
                }
            }
        }
    }

    /* access modifiers changed from: protected */
    @Override // androidx.appcompat.app.AppCompatActivity, androidx.fragment.app.FragmentActivity, com.androidx.p013lv.base.p015ui.BaseActivity
    public void onDestroy() {
        clearCache();
        SendDynamicModel sendDynamicModel = this.model;
        if (sendDynamicModel != null) {
            sendDynamicModel.cancelHttp();
        }
        super.onDestroy();
    }

    public class OnClick {
        public OnClick() {
        }

        public void btnBack() {
            SendDynamicActivity.this.finish();
        }

        /* access modifiers changed from: package-private */
        public void selectImgDynamic() {
            SendDynamicActivity.this.requestPicturePermission(9);
        }

        /* access modifiers changed from: package-private */
        public void selectVideoDynamic() {
            SendDynamicActivity.this.requestVideoPermission();
        }

        public void sendUserDynamic() {
            if (!SendDynamicActivity.this.isOnClick()) {
                String trim = ((ActivitySendDynamicLayoutBinding) SendDynamicActivity.this.binding).tvEdit.getText().toString().trim();
                if (TextUtils.isEmpty(trim)) {
                    ToastUtils.getInstance().showSigh("請輸入內容");
                } else if (((ActivitySendDynamicLayoutBinding) SendDynamicActivity.this.binding).getType().intValue() == 0) {
                    if (SendDynamicActivity.this.imageList == null || SendDynamicActivity.this.imageList.size() == 0 || SendDynamicActivity.this.imageList.size() == 1) {
                        ToastUtils.getInstance().showSigh("請選擇圖片");
                        return;
                    }
                    SendDynamicActivity.this.showProgressBarDialog("正在發布，請稍後...");
                    SendDynamicActivity.this.model.sendImg(SendDynamicActivity.this.imageList, trim, 0);
                } else if (TextUtils.isEmpty(SendDynamicActivity.this.videoPath)) {
                    ToastUtils.getInstance().showSigh("請選擇視頻");
                } else {
                    SendDynamicActivity.this.sendModel.send(SendDynamicActivity.this.videoPath);
                }
            }
        }
    }
}
